// when the page gets shown the current level is loaded from local storage
$( document ).delegate("#levelpage", "pageshow", function() {
	var currentLevel = parseInt(localStorage.currentLevel);
	var currentTheme = 6;//parseInt(localStorage.currentTheme); <-- must be added later
	loadLevel(currentLevel, currentTheme);
});

// intervalID holds a reference to the update interval (box2Dweb)
// intervalID is set in the levelX.js files
var intervalID;
function stopUpdateFunction() {
	if(!(intervalID == null)) {
		window.clearInterval(intervalID); //intervalID is set in levelX.js where X is a number
		intervalID = null;
	}
}

//reloads the page
function reloadPage() {
	stopUpdateFunction();
	
	location.reload();
}

//reset the canvas to show other stuff
function resetCanvas() {
	stopUpdateFunction();
	
	var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');
    context.clearRect(0, 0, canvas.width, canvas.height);
}

//this gets called from the levelX.js files
function levelFinished() {
	//todo: save data
	$( "#popup" ).popup( "open" );
}

//open the next level and save new level index
function next() {
	$( "#popup" ).popup( "close" );
	var currentLevel = parseInt(localStorage.currentLevel);
	localStorage.currentLevel = "" + ++currentLevel;
	
	reloadPage();
	
	/*
	url = location.protocol + '//' + location.host + location.pathname;
	$.mobile.changePage(
		"#",
	    {
	      allowSamePageTransition : true,
	      transition              : 'slide',
	      showLoadMsg             : false,
	      reloadPage              : true
	    }
	); */ 
}

//open the menu
function menu() {
	stopUpdateFunction();
	$.mobile.changePage(
		"levelselector.html",
	    {
	      transition       	: 'pop',
	      reverse			: true
	    }
	);
	resetCanvas();
}

function clearTimeInterval(){
	clearInterval(level1Timer);
	clearInterval(level2Timer);
	clearInterval(level3Timer);
	clearInterval(level4Timer);
	clearInterval(level5Timer);
	clearInterval(level6Timer);
	clearInterval(level7Timer);
	clearInterval(level8Timer);
	clearInterval(level9Timer);
}

//load the javascript file and the description of the level and execute the javascript file
function loadLevel ( level, theme ) {
	resetCanvas();
	
//	$.when(
//			$.getScript("js/theme6/level9.js"),
//			$.Deferred(function( deferred ){
//				$(deferred.resolve);
//			})
//	
//	).done(function(){
		var url = "js/theme6/level" + level + ".js"; 
		console.log(level);
		switch(level){
			case 1:
				$.getScript( url, function(){
					clearTimeInterval();
					level1();
					$('#leveltxt').text("LEVEL 1");
					$('#leveldescription').text("Instructions: Use the bolt to move the egg and make it go to the basket.");
				});
				break;
			case 2:
				$.getScript( url, function(){
					clearTimeInterval();
					level2();
					$('#leveltxt').text("LEVEL 2");
					$('#leveldescription').text("Instructions: Use the bolt to move the egg and make it go to the basket.");
				});
				break;
			case 3:
				$.getScript( url, function(){
					clearTimeInterval();
					level3();
					$('#leveltxt').text("LEVEL 3");
					$('#leveldescription').text("Instructions: Use the bolt to move the egg and make it go to the basket. ");
				});
				break;
			case 4:
				$.getScript( url, function(){
					clearTimeInterval();
					level4();
					$('#leveltxt').text("LEVEL 4");
					$('#leveldescription').text("Instructions: Use the bolt to move the egg and make it go to the basket. ");
				});
				break;
			case 5:
				$.getScript( url, function(){
					clearTimeInterval();
					level5();
					$('#leveltxt').text("LEVEL 5");
					$('#leveldescription').text("Instructions: Drag and drop the egg in the basket.");
				
				});
				break;
			case 6:
//				$.getScript( url, function(){
//					$.when(
//						$.getScript("theme6/createjs/utils/Proxy.js"),
//						$.getScript("theme6/createjs/utils/IndexOf.js"),
//						$.getScript("theme6/createjs/events/Event.js"),
//						$.getScript("theme6/createjs/events/EventDispatcher.js"),
//						$.getScript("theme6/soundjs/Sound.js"),
//						$.getScript("theme6/soundjs/WebAudioPlugin.js"),
//						$.getScript("theme6/soundjs/HTMLAudioPlugin.js"),
//						$.Deferred(function( deferred ){
//							$(deferred.resolve);
//						})).done(function(){
				$.getScript( url, function(){
					
							clearTimeInterval();
							level6();
							$('#leveltxt').text("LEVEL 6"); // Old 10
							$('#leveldescription').text("Instructions: Use the bolt to move the egg and make it go to the basket. ");
				});

//						});
//				});
				break;
			case 7:
				$.getScript( url, function(){
					clearTimeInterval();
					level7();
					$('#leveltxt').text("LEVEL 7");
					$('#leveldescription').text("Instructions: Use the bolt to move the egg and make it go to the basket. ");
				});
				break;
			case 8:
				$.getScript( url, function(){
					clearTimeInterval();
					level8();
					$('#leveltxt').text("LEVEL 8");
					$('#leveldescription').text("Instructions: Use the bolt to move the egg and make it go to the basket. ");
					
				});
				break;
			case 9:
				$.getScript( url, function(){
					clearTimeInterval();
					level9();
					$('#leveltxt').text("LEVEL 9");
					$('#leveldescription').text("Instructions: Use the bolt to move the egg and make it go to the basket. ");
				});
				break;
		}
		
//	})
	//level = 6; //for debugging
//	var url = "js/theme" + theme + "/level" + level + ".js";
//	$.getScript( url, function() {
//		var description = levelInfo[theme-1][level-1][0];
//		var width = levelInfo[theme-1][level-1][1];
//		var height = levelInfo[theme-1][level-1][2];
//		
//		var context = canvas.getContext('2d');
//		context.canvas.width = width;
//		context.canvas.height = height;
//		
//		console.log("loadLevel called");
//		
//		window["level" + level]();
//		$('#leveltxt').text("LEVEL " + level);
//		$('#leveldescription').text(description);			  
//	});
}

